package org.iwidget.desktop.model;
import org.w3c.dom.*;
/**
 *
 * @author Muhammad Hakim A
 */

public class WidgetMenuElement
{

    public WidgetMenuElement(WidgetElement widget)
    {
        this.widget = widget;
    }

    public void addMenu(String title, String function)
    {
        this.title = title;
        script = function;
    }

    public String getScript()
    {
        return script;
    }

    public void setScript(String string)
    {
        script = string;
    }

    public String getTitle()
    {
        return title;
    }

    public void setTitle(String string)
    {
        title = string;
    }

    public void parse(Node windowNode)
        throws Exception
    {
        NamedNodeMap map = windowNode.getAttributes();
        for(int iCtr = 0; iCtr < map.getLength(); iCtr++)
        {
            Node attr = map.item(iCtr);
            parseHelper(attr.getNodeName(), attr.getNodeValue());
        }

        NodeList nodes = windowNode.getChildNodes();
        for(int iCtr = 0; iCtr < nodes.getLength(); iCtr++)
        {
            Node childNode = nodes.item(iCtr);
            String nodeName = childNode.getNodeName();
            String nodeValue = childNode.getNodeValue();
            if(childNode.getFirstChild() != null)
                parseHelper(nodeName, childNode.getFirstChild().getNodeValue());
        }

    }

    public void parseHelper(String nodeName, String value)
    {
        if(nodeName.equals("title"))
            setTitle(value);
        else
        if(nodeName.equals("script"))
            setScript(value);
    }

    private String title;
    private String script;
    private WidgetElement widget;
}